package com.frame.database.proxy;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;

import com.frame.database.DataException;
import com.frame.database.operate.JdbcDao;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2015
 * @Company:
 * @author 王海锋
 * @CreatedTime:2015-6-26 上午09:40:59
 * @version 1.0
 */

public class JServiceProxy implements MethodInterceptor{

    private JdbcDao serviceDao;
    public Class<JdbcDao> daoClass;
    private static final ThreadLocal<Map<String,Boolean>> transHolder = new ThreadLocal<Map<String,Boolean>>();
//    private Map<String,Boolean> trans=new HashMap<String,Boolean>();

    public void setDaoClass(Class<JdbcDao> d){
        this.daoClass=d;
    }
    public Object intercept(Object obj, Method method, Object[] pams, MethodProxy proxy) throws Throwable {
        // TODO Auto-generated method stub
        Object o=null;

        String methodName=method.getName();
        if("finalize".equals(methodName)){
            o=proxy.invokeSuper(obj,pams);
        }else if("getDao".equals(methodName)){
            if(serviceDao==null){
                serviceDao=daoClass.newInstance();
            }
            return serviceDao;
        }else{
            if(serviceDao==null){
                serviceDao=daoClass.newInstance();
            }
            try{
                methodName=serviceDao.getClass()+"."+methodName;
//            	if(method.isAnnotationPresent(Transactional.class)){
////            		System.out.println("openTransactional");
//	                Transaction tran=serviceDao.getSession().getTransaction();
//	                if(tran==null || !tran.isActive()){
//	                    serviceDao.getSession().beginTransaction();
//	                    getTransMap().put(methodName, true);
//	                }
//            	}
                o=proxy.invokeSuper(obj,pams);
//                Boolean haveTran=getTransMap().get(methodName);
//                if(haveTran!=null && haveTran){//谁启动了事务，谁关闭事务
////                	System.out.println(methodName+"--commit:");
//                    serviceDao.getSession().commit();
//                }
            }
            catch(Exception e){
//                Boolean haveTran=getTransMap().get(methodName);
//                if(haveTran!=null && haveTran)
//                    serviceDao.getSession().rollback();
                throw new DataException(e.getMessage(),e);
            }finally{
//                Boolean haveTran=getTransMap().get(methodName);
//                boolean closes=false;
//                if(haveTran!=null && haveTran)closes=true;//如果开启事务，则强制关闭session
//                System.out.println("");
//                System.out.print(methodName+"-closeTransactional:"+closes);
//                System.out.println("");
//                System.out.print(methodName);
                serviceDao.getSession().closeSession(true);
//                try{
//                	getTransMap().remove(methodName);
//                }catch(Exception ev){}
            }
        }
        return o;
    }

    private static Map<String,Boolean> getTransMap(){
        Map<String,Boolean> transMap=transHolder.get();
        if(transMap==null){
            transMap=new HashMap<String, Boolean>();
            setTransMap(transMap);
        }
        return transMap;
    }
    private static void setTransMap(Map<String,Boolean> transMap) {
        transHolder.set(transMap);
    }
}
